package com.hoshiicloud.stock.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.controller.BaseController;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.stock.entity.WarehouseList;
import com.hoshiicloud.stock.param.WarehouseListPageParam;
import com.hoshiicloud.stock.param.WarehouseListUpdateParam;
import com.hoshiicloud.stock.param.WarehousePageParam;
import com.hoshiicloud.stock.service.IWarehouseListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 库存Controller类
 *
 * @author : yangcheng
 * @date : 2019/5/21 14:49
 * @mail : maiboyc@163.com
 **/
@Slf4j
@RestController
@RequestMapping("/warehouseList")
@Api(value = "库存管理", tags = {"库存管理操作接口"}, produces = MediaType.APPLICATION_JSON_VALUE)
public class WarehouseListController extends BaseController {

    // 库存Service
    @Autowired
    private IWarehouseListService warehouseListService;

    /**
     * 根据ID查询库存信息
     * @param id 库存ID
     * @return Response
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询", notes = "根据ID查询库存信息", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParam(name = "id", value = "库存主键ID", required = true, dataType = "Long", example = "3187243209717710848", paramType = "query")
    public Response<WarehouseList> getById(@PathVariable("id") Long id) {
        Response<WarehouseList> rs;
        try {
            rs = Response.data(warehouseListService.getById(id));
        } catch (Exception e) {
            log.error("根据ID查询库存信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }

    /**
     * 根据ID组查询库存信息
     * @param ids ID组
     * @return Response
     */
    @GetMapping("/getByIds")
    @ApiOperation(value = "根据ID组查询", notes = "根据ID组查询库存信息", consumes = MediaType.ALL_VALUE)
    public Response<List<WarehouseList>> getByIds(@RequestParam("ids") List<Long> ids) {
        Response<List<WarehouseList>> rs;
        try {
            rs = Response.data(warehouseListService.getByIds(ids));
        } catch (Exception e) {
            log.error("根据ID组查询库存信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }
    /**
     * 分页查询库存信息（post请求，以对象接收参数）
     * @param param 查询参数实体
     * @return Response
     */
    @PostMapping("/list")
    @ApiOperation(value = "分页查询", notes = "分页查询库存信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response<IPage<WarehouseList>> list(@RequestBody WarehouseListPageParam param) {
        Response<IPage<WarehouseList>> rs;
        try {
            rs = Response.data(warehouseListService.List(param));
        } catch (Exception e) {
            log.error("分页查询库存信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }

    /**
     * 新增库存
     * @param entity 保存参数实体
     * @return Response
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增库存", notes = "新增库存信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response<Long> save(@RequestBody @Valid WarehouseList entity, BindingResult result) {
        Response<Long> rs;
        try {
            if (CollectionUtils.isNotEmpty(handleValidMsg(result))){// 验证不通过
                rs = Response.failed(JSON.toJSONString(handleValidMsg(result)));
            } else {// 验证通过
                rs = Response.data(warehouseListService.insert(entity));
            }
        } catch (ServiceException e) {
            log.error("新增库存信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getError());
        }catch (Exception e) {
            log.error("新增库存信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }

    /**
     * 修改库存
     * @param param 修改参数实体
     * @return Response
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改库存", notes = "修改库存信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response<Long> update(@RequestBody @Valid WarehouseListUpdateParam param, BindingResult result) {
        Response<Long> rs;
        try {
            if (CollectionUtils.isNotEmpty(handleValidMsg(result))){// 验证不通过
                rs = Response.failed(JSON.toJSONString(handleValidMsg(result)));
            } else {// 验证通过
                WarehouseList entity = new WarehouseList();
                BeanUtils.copyProperties(param, entity);
                rs = Response.data(warehouseListService.update(entity));
            }
        } catch (ServiceException e) {
            log.error("更新库存信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getError());
        } catch (Exception e) {
            log.error("更新库存信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }

    /**
     * 根据ID删除库存信息
     * @param id 库存ID
     * @return Response
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除库存信息", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParam(name = "id", value = "库存主键ID", required = true, dataType = "Long", example = "3187243209717710848", paramType = "query")
    public Response<Boolean> delete(@PathVariable("id") Long id) {
        Response<Boolean> rs;
        try {
            rs = Response.data(warehouseListService.delete(id));
        } catch (ServiceException e) {
            log.error("删除库存信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getError());
        } catch (Exception e) {
            log.error("删除库存信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }
}  